﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Customer_Profile.eCard.Settle
{
    public partial class Settle_list : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntityID = "";
        string strEntity = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
            if (!Page.IsPostBack)
            {
                DataSet dsEntity = EntityDao.getEntityList("N");
                dropEntity.DataSource = dsEntity;
                dropEntity.DataTextField = "entity_name";
                dropEntity.DataValueField = "entity_id";
                dropEntity.DataBind();
                ListItem liAllEntity = new ListItem();
                liAllEntity.Text = "All";
                liAllEntity.Value = "All";
                dropEntity.Items.Insert(0, liAllEntity);
                
                dropEntity.Items.FindByValue(strEntityID).Selected = true;
                dropEntity.Enabled = false;

                Comm.BindDll(ref dropSettleStatus, "SettleStatus", true, "");

                txtcreatedatebegin.Text = DateTime.Now.ToString("yyyy-MM") + "-01";
                txtcreatedateend.Text = DateTime.Parse(DateTime.Now.AddMonths(1).ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd");
            }
            bindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void bindGrid()
        {
            string strStartDate = txtcreatedatebegin.Text.Trim();
            string strEndDate = txtcreatedateend.Text.Trim();

            if (strStartDate.Equals("") || strEndDate.Equals(""))
            {
                Response.Write("起始时间和结束时间不能为空");
            }

            string strWhere = " where 1=1 ";
            if (dropEntity.SelectedIndex > 0)
                strWhere += " and d.entity_id = '" + dropEntity.SelectedItem.Value.Trim() + "'";

            if (dropSettleStatus.SelectedIndex > 0)
            {
                strWhere += " and ss.settle_status = '" + dropSettleStatus.SelectedItem.Value + "'";
            }
            if (txtcreatedatebegin.Text != "" && txtcreatedateend.Text != "")
                strWhere += " and convert(date,d.due_date) between '" + txtcreatedatebegin.Text.Trim() + "' and '" + txtcreatedateend.Text.Trim() + "'";

            strWhere += " ORDER BY deal_no DESC";

            DataSet ds = DealDao.getCardSettleDeals(strWhere);

            GvData.DataSource = ds;
            GvData.DataBind();
        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvData.PageIndex = e.NewPageIndex;
            bindGrid();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

                LinkButton lbcardno = (LinkButton)e.Row.Cells[1].Controls[1];
                lbcardno.Attributes.Add("onclick", "javascript:window.open('/eCard/CardDetail.aspx?cardno=" + lbcardno.Text + "');return false");

                string strSettleID = GvData.DataKeys[e.Row.RowIndex].Values["settle_id"].ToString();
                string strStatus = GvData.DataKeys[e.Row.RowIndex].Values["settle_status"].ToString();

                string input_userid = GvData.DataKeys[e.Row.RowIndex].Values["input_userid"].ToString();   

                LinkButton lbAction = (LinkButton)e.Row.Cells[11].Controls[1];
                //添加修改
                if (strStatus.Equals("SI") || strStatus.Equals("SB") && strUserID.Trim().Equals(input_userid.Trim()))
                {
                    lbAction.Text = "修改";
                    lbAction.Attributes.Add("onclick", "javascript:window.location.href='settle_application.aspx?settle_id=" + strSettleID + "';return false");
                }
                else
                {
                    lbAction.Text = "";
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //标题
            byte[] bTitle = System.Text.Encoding.GetEncoding("UTF-8").GetBytes("结算信息" + "<br><br>");
            StringBuilder sb = new StringBuilder();
            sb.Append(System.Text.Encoding.GetEncoding("UTF-8").GetString(bTitle));

            //报表内容
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            GvData.AllowPaging = false;
            GvData.ShowHeader = true;
            bindGrid();
            GvData.RenderControl(oHtmlTextWriter);
            byte[] bContent = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(oStringWriter.ToString());
            sb.Append(System.Text.Encoding.GetEncoding("UTF-8").GetString(bContent));

            //输出
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=settle.xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            System.Web.HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";    //设置输出文件类型为excel文件。 
            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=" + "UTF-8" + "\" >");

            string strExcel = sb.ToString();

            //去除超链接 
            strExcel = Regex.Replace(strExcel, "<a (.*?)>", "", RegexOptions.Compiled);
            strExcel = Regex.Replace(strExcel, "</a>", "");

            System.Web.HttpContext.Current.Response.Write(strExcel);
            System.Web.HttpContext.Current.Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }
}